#ifndef RENDERNST_H
#define RENDERNST_H

#include <GL/gl.h>
#include <QObject>

#include <QGLWidget>
#include <QOpenGLFunctions>



#include "core/api/NstApiMachine.hpp"
#include "core/api/NstApiVideo.hpp"
#include "core/api/NstApiSound.hpp"
#include "core/api/NstApiInput.hpp"
#include "core/api/NstApi.hpp"

class NstInFboRenderer;

namespace LinuxNst
{
    class Settings;
}

namespace Nes
{
    namespace Api
    {

    }
}


using namespace Nes::Api;
using namespace LinuxNst;

class RenderNst : public QObject
{
    Q_OBJECT
public:
    RenderNst();
    // load a game or NES music file
    void NstLoadGame(const char* filename);
    void NstPlayGame(void);
    int openglInitStructures(QOpenGLFunctions *ptr = NULL);
    void opengl_blit(QOpenGLFunctions *ptr = NULL);
    void opengl_cleanup(QOpenGLFunctions *ptr = NULL);


    long Linux_LockScreen(void*& ptr);
    void Linux_UnlockScreen(void*);

    void updateEmulator();

protected:

protected:
    void SetupVideo();
protected:
    void configure_savename( const char* filename );
    void nst_unload(void);
    Machine::FavoredSystem get_favored_system(void);

    int powerOfTwo( const int value );

public:
    int nsf_mode;
    int loaded;
    int framerate;
    int cur_width, cur_height;
    int cur_Rwidth, cur_Rheight;
    int gl_w, gl_h;
    int playing;
    bool linear_filter;
    void* intbuffer;

    NstInFboRenderer *pNstInFboRenderer;

    GLuint  screenTexID;

    Video::Output *cNstVideo;
    Sound::Output *cNstSound;
    Input::Controllers *cNstPads;

    Settings *sSettings;
};

class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    GLWidget()
    {
        startTimer(10);
    }

protected:
    void initializeGL()
    {
        nst.openglInitStructures();
    }

    void paintGL()
    {
        nst.updateEmulator();
        nst.opengl_blit();
    }
    virtual void timerEvent(QTimerEvent *)
    {
        update();
    }

public:
    RenderNst nst;
};


#endif // RENDERNST_H
